package com.csdj.t309springboot.config;
import com.csdj.t309springboot.constant.SessionConstant;
import com.csdj.t309springboot.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {


    private RedisService2 redisService2;
    private Logger log = LoggerFactory.getLogger("test");
    public LoginInterceptor(RedisService2 redisService2) {
        this.redisService2 = redisService2;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("LoginInterceptor => 执行拦截器 preHandle()");
        log.info("preHandle() user="+request.getSession().getAttribute(SessionConstant.USER_SESSION));
        User user = (User) request.getSession().getAttribute(SessionConstant.USER_SESSION);
        if(null != user){
            return true;

        }
        response.sendRedirect("/login");
        return false;
//        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception {

        //        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }
}
